package cn.org.zx.common.dao;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.org.zx.db.dbexception.DBErrorException;

public abstract class ResultSetUtils{
		
	/**
	 * 将结果集的一行放到map中
	 * @param crs
	 * @return
	 * @throws DBErrorException
	 */ 
	public static Map ResultSetToMap(ResultSet rs) throws DBErrorException{
		String strFiledName = "";			//字段名

		ResultSetMetaData metaData = null;
		Map<String, Object> mapRow = null;
		try {
			metaData = rs.getMetaData();
			mapRow = new HashMap<String, Object>(metaData.getColumnCount());
			for (int i = 1; i <= metaData.getColumnCount(); i++) {
				strFiledName = metaData.getColumnName(i).toLowerCase();
				mapRow.put(strFiledName,rs.getObject(strFiledName));
			}
			return mapRow;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DBErrorException("RowSetUtils.rowsetToMap()异常");
		}
		
	}
	
	/**
	 * 将结果集放到list中
	 * @param crs
	 * @return
	 * @throws DBErrorException
	 */
	public static List ResultSetToList(ResultSet rs) throws DBErrorException{
		List<Map> listReturn = new ArrayList<Map>();
		Map mapRow = null;
		try {
			while (rs.next()) {
				mapRow = ResultSetToMap(rs);
				listReturn.add(mapRow);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listReturn;
	}
	
	/**
	 * @param args
	 * @throws DBErrorException 
	 * @throws SQLException 
	 */
	public static void main(String[] args) {
		
	}
}
